package com.architecture.core.service;

import com.architecture.core.dao.BaseDAO;
import com.architecture.core.entity.Config;
import com.architecture.core.utils.ConfigUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-21
 * Time: 下午2:30
 * To change this template use File | Settings | File Templates.
 */
@Service("configService")
public class ConfigService extends AbstractService {

    public enum ConfigName{
        MAX_CODE;
    }

    @javax.annotation.Resource
    private BaseDAO baseDAO;

    private Map<String,String> configMap = new HashMap<String, String>();

    public List<String> getAllName(){
        return new ArrayList<String>(configMap.keySet());
    }

    public List<String> getAllValue() {
        return new ArrayList<String>(configMap.values());
    }

    public String getConfigValue(ConfigName name) {
        if (configMap.isEmpty()) {
            this.initConfigMap();
        }

        return configMap.get(name.toString());
    }

    private void initConfigMap() {

        configMap.clear();

        List<Config> configList = (List<Config>)this.baseDAO.find(ConfigUtil.getString(ConfigUtil.D_QUERY_ALL_CONFIG));

        for (Config config : configList) {
            configMap.put(config.getName(), config.getValue());
        }
    }

}
